package org.iteam.xlz.core.service.ad;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.ad.AdInfoVo;
import org.iteam.xlz.core.entity.ad.AdStrategyVo;

public class AdInfoService extends BaseService<AdInfoVo>{
	static AdInfoService instance;
	
	private AdInfoService(){
	}
	
	public static AdInfoService getInstance(){
		if(null==instance){
			instance=new AdInfoService();
		}
		return instance;
	}
	
	public long counts(){
		try {
			return super.counts("select count(1) from adinfo where 1=1");
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<AdInfoVo> query(long page,long rows){
		try {
			return super.query("select * from adinfo where 1=1 limit ?,?",(page-1)*rows,rows);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<AdInfoVo>();
		}
	}
	
	
	public boolean create(AdInfoVo model){
		model.setCreateTime(new Date());
		SqlBuilder builder=SqlBuilder.createInsert("adinfo");
		builder.addField("title",model.getTitle());
		builder.addField("content",model.getContent());
		builder.addField("imgurl",model.getImgurl());
		builder.addField("linkuri",model.getLinkuri());
		builder.addField("jumpType",model.getJumpType());
		builder.addField("isEnable",model.getIsEnable());
		builder.addField("plat",model.getPlat());
		builder.addField("customCofig",model.getCustomCofig());
		builder.addField("createTime",model.getCreateTime());
		builder.addField("creator",model.getCreator());
		try {
			return super.update(builder.getSql(),builder.getParams());
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(AdInfoVo model){
		SqlBuilder builder=SqlBuilder.createUpdate("adinfo");
		builder.addField("title",model.getTitle());
		builder.addField("content",model.getContent());
		builder.addField("imgurl",model.getImgurl());
		builder.addField("linkuri",model.getLinkuri());
		builder.addField("jumpType",model.getJumpType());
		builder.addField("isEnable",model.getIsEnable());
		builder.addField("plat",model.getPlat());
		builder.addField("customCofig",model.getCustomCofig());
		builder.addWhere("id",model.getId());
		try {
			return super.updates(builder.getSql(),builder.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	
	public AdInfoVo findById(String id){
		try {
			return super.show("select * from adinfo where 1=1 and id=?",id);
		} catch (SQLException e) {
			e.printStackTrace();
			return new AdInfoVo();
		}
	}
	
	public boolean delete(String id){
		try {
			return super.updates("delete from adinfo where id=?",id)>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public List<AdInfoVo> getAdInfoList(){
		try {
			return super.query("select * from adinfo where 1=1");
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<AdInfoVo>();
		}
	}
}
